<?php
session_start();
require_once __DIR__.'/../../utils/checkTokenUtils.php';
require_once __DIR__.'/../../model/BorrowModel.php';
require_once __DIR__.'/../../utils/MysqlDBUtils.php';
require_once __DIR__.'/../../model/UserModel.php';
require_once __DIR__.'/../../model/BookModel.php';
use model\BorrowModel;
use utils\MysqlDBUtils;
use model\UserModel;
use model\BookModel;
$dbUtil=new MysqlDBUtils();

$successMessage=$_SESSION['success_message']?? null;
$errorMessage=$_SESSION['error_message']?? null;

$borrowModel=new BorrowModel();
$userModel=new UserModel();
$bookModel=new BookModel();
$users=$userModel->getAllUsers();
$books=$bookModel->getAllBooks();
$q=isset($_GET['q'])? trim($_GET['q']) : '';
$currentPage=max(1,intval($_GET['page']??1));
$perPage=max(1,min(intval($_GET['per_page']??8),100));
$paginationData=$borrowModel->getPaginatedBorrow($q,$currentPage,$perPage);

extract($paginationData);

$title='借阅列表';
ob_start();
?>

    <!-- 成功/错误提示 -->
<?php if ($successMessage): ?>
    <div id="successNotice" class="fixed top-20 left-1/2 transform -translate-x-1/2 bg-green-100 border border-green-400 text-green-700 px-4 py-2 rounded-lg shadow-lg max-w-md">
        <?= htmlspecialchars($successMessage) ?>
    </div>
<?php endif; ?>

<?php if ($errorMessage): ?>
    <div id="errorNotice" class="fixed top-20 left-1/2 transform -translate-x-1/2 bg-red-100 border border-red-400 text-red-700 px-4 py-2 rounded-lg shadow-lg max-w-md">
        <?= htmlspecialchars($errorMessage) ?>
    </div>
<?php endif; ?>

    <!-- 清除已显示的消息 -->
<?php
unset($_SESSION['success_message'], $_SESSION['error_message']);
?>

<div class="mb-4 flex gap-2 items-center justify-between">
    <button id="addBorrowBtn" class="add-btn px-3 py-1 bg-blue-500 text-white rounded-md hover:bg-blue-600 transition-colors ">新增借阅记录</button>
</div>

<table class="w-full">
    <thead class="bg-gray-100">
    <tr class="bg-gray-100">
<!--        <th class="p-3">序号</th>-->
        <th class="p-3">ISBN</th>
        <th class="p-3">书名</th>
        <th class="p-3">作者</th>
        <th class="p-3">出版社</th>
        <th class="p-3">出版日期</th>
        <th class="p-3">价格</th>
        <th class="p-3">借阅用户</th>
        <th class="p-3">借阅时间</th>
        <th class="p-3">归还时间</th>
        <th class="p-3">是否归还</th>
        <th class="p-3">操作</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($borrows as $borrow):?>
    <tr class="border-b">
        <input
            type="hidden"
            name="id"
            value="<?= htmlspecialchars($borrow['borrow_id'])?>"
        >
<!--        <td class="p-3 text-center">--><?php //$i=1;echo $i;$i++?><!--</td>-->
        <td class="p-3 text-center"><?php echo  htmlspecialchars($borrow['isbn'])?></td>
        <td class="p-3 text-center"><?php echo htmlspecialchars($borrow['title'])?></td>
        <td class="p-3 text-center"><?php echo htmlspecialchars($borrow['author'])?></td>
        <td class="p-3 text-center"><?php echo htmlspecialchars($borrow['publisher'])?></td>
        <td class="p-3 text-center"><?php echo htmlspecialchars($borrow['publish_date'])?></td>
        <td class="p-3 text-center"><?php echo htmlspecialchars($borrow['price'])?></td>
        <td class="p-3 text-center"><?php echo htmlspecialchars($borrow['name'])?></td>
        <td class="p-3 text-center"><?php echo htmlspecialchars($borrow['borrow_time'])?></td>
        <td class="p-3 text-center"><?php echo htmlspecialchars($borrow['return_time'])?></td>
        <td class="p-3 text-center"><?php echo htmlspecialchars($borrow['status']==1?'已归还':'未归还');  ?></td>
        <td class="p-3 text-center">
            <button
                class="edit-btn px-3 py-1 bg-blue-500 text-white rounded-md hover:bg-blue-600 transition-colors mr-2"
                data-id="<?= htmlspecialchars($borrow['borrow_id']) ?>"
                data-return-time="<?= htmlspecialchars($borrow['return_time'] ?? '') ?>"
                data-status="<?= htmlspecialchars($borrow['status']) ?>"
               >
                编辑
            </button>
            <button class="delete-btn px-3 py-1 bg-red-500 text-white rounded-md hover:bg-red-600 transition-colors"
                    data-delete-url="DeleteBorrow.php?id=<?= htmlspecialchars($borrow['borrow_id']) ?>">删除
            </button>
        </td>
    </tr>
    <?php endforeach; ?>
    </tbody>
</table>


<?php include __DIR__ . '/../partials/pagination.php'; ?>
<?php include __DIR__ . '/partials/add_modal.php'; ?>
<?php include __DIR__.'/../partials/del_modal.php';?>
<?php include __DIR__.'/partials/edit_modal.php';?>
<?php
$content = ob_get_clean();
include __DIR__ . '/../layouts/Main.php'; // 引入主布局
?>

<script>
    function deleteBorrow(borrowId){
        if (confirm('确定要删除这条借阅记录吗？')){
            const form = document.createElement('form');
            form.method = 'post';
            form.action = 'DeleteBorrow.php';

            const idInput = document.createElement('input');
            idInput.type = 'hidden';
            idInput.name = 'id';
            idInput.value = borrowId;
            form.appendChild(idInput);

            document.body.appendChild(form);
            form.submit();
        }
    }
</script>
